<?php
class SDKTimer
{
	private $_method;
	private $_args;
	private $_timer;
	private $_elapsed;
	private $_logFileName;

	public function __construct()
	{
		$args = func_get_args();
		$method = isset($args[0])?$args[0]:'';
		assert(''!=$method);
		unset($args[0]);

		$this->_method = $method;
		$this->_args = $args;
		$this->_timer = new Timer();
		$this->_elapsed = false;
		
		$conf = QFrameConfig::getConfig('EXTERNALS');
		$this->_logFileName = $conf['logPath'] . 'sdkTimer.log.'.Date('Ymd');
	}

	public function start()
	{
		$this->_timer->start();
	}

	public function elapsed()
	{
		$cost = 1000*$this->_timer->elapsed();
		$msg = $this->formatMsg($cost);
		error_log($msg, 3, $this->_logFileName);
		$this->_elapsed = true;
	}

	public function stop()
	{
		if (false == $this->_elapsed)
		{
			$this->elapsed();
		}
		$this->_timer->stop();
	}

	static private function sdkPrepareArgs($args)
	{
		if (is_array($args))
		{
			if (empty($args))
			{
				return '';
			}
			else
			{
				return implode(',', $args);
			}
		}
		return $args;
	}

	static private function prepareArgs($args)
	{
		//tmp
		return serialize($args);
		if ( false == empty($args) && is_array($args) )
		{
			$v = array_map(array('self', 'sdkPrepareArgs'), $args);
			if (is_array($v) && false == empty($v))
			{
				return implode(',', $v);
			}
			return $v;
		}
		else
		{
			return $args;
		}
	}

	private function formatMsg($cost)
	{
		$msg = '';

		$str = self::prepareArgs($this->_args);

		$msg = Date('H:i:s').' | method: '.$this->_method.' | cost: '.$cost;

		if ('' != $str)
		{
			$msg .= ' | args: '.$str;
		}
		return $msg . "\n";
	}
}
?>